const express = require('express');
const router = express.Router();
const Shortmes = require("./shortmes");
const Regmes = require("./regmes");
const Article = require("./article");
const Product = require("./product");
const Chatmes = require("./chatmes");

const validate = require("../public/javascripts/validate");

/* GET home page. */
router.get('/', function(req, res, next) {
  const user = res.locals.user = req.session.user||null;
  // console.log("user=",user);
  res.render('index');
});

router.get('/reg', function(req, res, next) {
  const user = res.locals.user = req.session.user||null;
  req.session.firstnum = res.locals.firstnum = Math.round(Math.random() * 10);
  req.session.secondnum = res.locals.secondnum = Math.round(Math.random() * 10);
  res.render("reg");
});

router.post("/reg", async function(req, res) {
  const {
    loginname,
    password,
    admin,
    confirm,
    vnum
  } = req.body;
  const firstnum = req.session.firstnum;
  const secondnum = req.session.secondnum;
  const errors = validate(loginname,password,confirm,vnum,firstnum,secondnum);
  if(errors){
    console.log(errors);
    res.send(errors);
  }else{
    //读取注册信息，得到已注册的用户名
    const regmeslist = await Regmes.find();
    console.log(regmeslist);
    //判断该用户名是否与已注册的用户名重复
    if (regmeslist){
      const len=regmeslist.length;
      for(j = 0; j < len; j++) {
        if (loginname === regmeslist[j].loginname) {
          console.log("此用户名已注册，请重新注册。");
          res.send("此用户名已注册，请重新注册。");
        }
      }
    }else{
      //将注册信息存入数据库中
      const reg = new Regmes({
        loginname,
        password,
        admin
      });
      await reg.save();
      console.log("reg=",reg);
      res.locals.user = req.session.user = reg;
      // console.log("req.session.user=",req.session.user);
      console.log("注册成功");
      res.send("注册成功");
    }
  }
});

router.get('/login', function(req, res, next) {
  const user = res.locals.user = req.session.user||null;
  req.session.firstnum = res.locals.firstnum = Math.round(Math.random() * 10);
  req.session.secondnum = res.locals.secondnum = Math.round(Math.random() * 10);
  res.render("login");
});

router.post("/login", async function(req, res) {
  const {
    loginname,
    password,
    vnum
  } = req.body;
  const firstnum = req.session.firstnum;
  const secondnum = req.session.secondnum;
  const errors = validate(loginname,password,password,vnum,firstnum,secondnum);
  if(errors){
    console.log(errors);
    res.send(errors);
  }else{
    //判断该用户名是否已注册
    const regmeslist = await Regmes.find();
    console.log("regmeslist=",regmeslist);
    const len=regmeslist.length;
    for(j = 0; j < len; j++) {
      if (loginname === regmeslist[j].loginname) {
        if (regmeslist[j].password === password) {
          res.locals.user = req.session.user = {loginname};
          console.log("req.session.user=",req.session.user);
          console.log("登录成功！");
          res.send("登录成功！");
        }else{
          console.log("用户密码输入错误，请重新输入。");
          res.send("用户密码输入错误，请重新输入。");
        }
      }
    }
    if(!req.session.user){
      console.log("此用户名尚未注册，请先注册。");
      res.send("此用户名尚未注册，请先注册。");
    }
  }
});

router.get('/shortmes', async function(req, res, next) {
  const user = res.locals.user = req.session.user||null;
  req.session.firstnum = res.locals.firstnum = Math.round(Math.random() * 10);
  req.session.secondnum = res.locals.secondnum = Math.round(Math.random() * 10);
  const page = Number(req.query.page) || 1;
  const shortmeslist = await Shortmes.find()
    .sort("-createTime")
    .limit(4)
    .skip((page-1)*4);
  // console.log("shortmeslist=",shortmeslist);
  const prevPage = page - 1 > 0 ? page -1 : 1;
  // console.log("prevPage=",prevPage);
  const nextshortmeslist = await Shortmes.find()
    .sort("-createTime")
    .limit(4)
    .skip(page*4);
  // console.log("nextshortmeslist=",nextshortmeslist);
  const nextPage = nextshortmeslist.length !== 0 ? page + 1 : page;
  console.log("nextPage=",nextPage);
  res.render('shortmes', {shortmeslist,prevPage,nextPage});
});

router.get('/article', async function(req, res, next) {
  const user = res.locals.user = req.session.user||null;
  req.session.firstnum = res.locals.firstnum = Math.round(Math.random() * 10);
  req.session.secondnum = res.locals.secondnum = Math.round(Math.random() * 10);
  const page = Number(req.query.page) || 1;
  const articlelist = await Article.find()
    .sort("-updateTime")
    .limit(4)
    .skip((page-1)*4);
  console.log("articlelist=",articlelist);
  const prevPage = page - 1 > 0 ? page -1 : 1;
  console.log("prevPage=",prevPage);
  const nextarticlelist = await Article.find()
    .sort("-updateTime")
    .limit(4)
    .skip(page*4);
  console.log("nextarticlelist=",nextarticlelist);
  const nextPage = nextarticlelist.length !== 0 ? page + 1 : page;
  console.log("nextPage=",nextPage);
  res.render('article', {articlelist,prevPage,nextPage});
});

router.get('/product', async function(req, res, next) {
  const user = res.locals.user = req.session.user||null;
  req.session.firstnum = res.locals.firstnum = Math.round(Math.random() * 10);
  req.session.secondnum = res.locals.secondnum = Math.round(Math.random() * 10);
  const page = Number(req.query.page) || 1;
  const productlist = await Product.find()
    .sort("-updateTime")
    .limit(4)
    .skip((page-1)*4);
  console.log("productlist=",productlist);
  const prevPage = page - 1 > 0 ? page -1 : 1;
  console.log("prevPage=",prevPage);
  const nextproductlist = await Product.find()
    .sort("-updateTime")
    .limit(4)
    .skip(page*4);
  console.log("nextproductlist=",nextproductlist);
  const nextPage = nextproductlist.length !== 0 ? page + 1 : page;
  console.log("nextPage=",nextPage);
  res.render('product', {productlist,prevPage,nextPage});
});


module.exports = router;
